package com.xca.is.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.xca.is.R;
import com.xca.is.databinding.ActivityTranslateBinding;
import com.xca.is.entity.Translation;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.TranslateModel;
import com.xca.is.utils.WebViewScriptUtils;

public class TranslateActivity extends AppCompatActivity implements HttpConnect {
    private ActivityTranslateBinding binding ;
    private ViewModelProvider provider ;
    private TranslateModel model ;
    private MutableLiveData<Translation> liveData ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTranslateBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.pbTranslate.setVisibility(View.GONE);

        int[] ints = new int[]{10,18} ;
        WebViewScriptUtils.htmlContent(ints,binding.webTranslate);

        provider = new ViewModelProvider(this) ;
        model = provider.get(TranslateModel.class);
        model.setModel(this);
        liveData = (MutableLiveData<Translation>) model.getLiveData() ;
        liveData.observe(this, new Observer<Translation>() {
            @Override
            public void onChanged(Translation translation) {
                if (translation.getCode()==200){
                    binding.tvTranslate.setText(translation.getData().getTranslate_result());
                }else {
                    binding.tvTranslate.setText("翻译失败");
                }
            }
        });

        onClickListener();
        setSpinner();

    }

    private void onClickListener()
    {
        binding.btnClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (binding.etTranslate.getText() == null){
                    binding.etTranslate.setError("请输入要翻译的内容");
                }else {
                    binding.etTranslate.setText("");
                    binding.etTranslate.clearFocus();
                    binding.tvTranslate.setText("");
                }
            }
        });

     binding.btnCopy.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             String copyText = binding.tvTranslate.getText().toString() ;
             if (copyText.isEmpty()){
                 binding.tvTranslate.setError("请先翻译");
             }else {
                 ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                 ClipData clip = ClipData.newPlainText("translate", copyText);
                 clipboard.setPrimaryClip(clip);
                 Toast.makeText(TranslateActivity.this, "文本已复制", Toast.LENGTH_SHORT).show();
             }

         }
     });

     binding.btnTranslate.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             String text =  binding.etTranslate.getText().toString() ;
             String from = getLanguage( binding.spinner1Translate.getSelectedItem().toString())  ;
             String to =  getLanguage(binding.spinner2Translate.getSelectedItem().toString())  ;
             model.getData(text,from,to);
         }
     });
    }



    private void setSpinner(){
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this,R.layout.translate_spinner_item,getResources().getStringArray(R.array.translates));
        binding.spinner1Translate.setAdapter(adapter1);
        adapter1.setDropDownViewResource(R.layout.translate_spinner_item);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this,R.layout.translate_spinner_item,getResources().getStringArray(R.array.translates2));
        binding.spinner2Translate.setAdapter(adapter2);
        adapter2.setDropDownViewResource(R.layout.translate_spinner_item);
        binding.spinner1Translate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String item = adapter1.getItem(i).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        binding.spinner2Translate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String item = adapter2.getItem(i).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    private String getLanguage(String language)
    {
        String text = "" ;
        switch (language){
            case "中文":
                text = "zh-CN";
                break;
            case "英语":
                text = "en";
                break;
            case "韩语":
                text = "ko";
                break;
            case "法语":
                text = "fr";
                break;
            case "俄语":
                text = "ru";
        }
        return text ;
    }

    @Override
    public void Loading(boolean loading) {
        binding.pbTranslate.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {

    }

    @Override
    public void Complete() {
        binding.pbTranslate.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}